package com.Babysleep.manage;

import android.os.Message;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.Babysleep.R;
import com.Babysleep.Activity.MaMaShuo;
import com.Babysleep.Interface.ListenHandler;

/**
 * 妈妈说播放进度控制
 * 
 */
public class MaMaShuoPlay extends Listen {
	private ProgressBar bar;
	private MaMaShuo activity;
	private MyHandler handler;

	

	public MaMaShuoPlay(MaMaShuo activity) {
		super(activity);
		this.activity = activity;
	}

	/**
	 * 初始化参数
	 */
	public void init(ImageButton button,String name) {
		super.init(button,name);
		bar = (ProgressBar) activity.findViewById(R.id.progressBar);
		bar.setMax(getDuration());
		bar.setProgress(0);
		handler = new MyHandler(Constant.delay_length);
	}

	public void UI() {
		bar.setProgress(getCurrentPosition());
	}

	public class MyHandler extends ListenHandler {

		public MyHandler(int delay) {
			super(delay);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				if (need_send == true) {
					UI();
					if (activity.cycle_length != 0)
						count();
					else
						((TextView) activity.findViewById(R.id.daojishi))
								.setText(df.format(0));
					sendEmptyMessageDelayed(1, delay);
				}
				break;

			default:
				break;
			}
		}

	}

	@Override
	public void Play() {
		// TODO Auto-generated method stub
		if (playing_started == false) {
			System.out.println("laile");
			handler.start();
		}
		super.Play();
	}

	@Override
	public void Pause() {
		// TODO Auto-generated method stub
		super.Pause();// 播放控制
		handler.pause();// handler控制
	}

	@Override
	public void Restart() {
		// TODO Auto-generated method stub
		super.Restart();
		handler.restart();
	}

	@Override
	public void Stop() {
		// TODO Auto-generated method stub
		super.Stop();
		handler.stop();
	}

	public void count() {
		super.count();
		System.out.println(counter);
		((TextView) activity.findViewById(R.id.daojishi)).setText(df
				.format(counter));
		if (counter <= 0) {
			((TextView) activity.findViewById(R.id.daojishi)).setText(df
					.format(0));
			Stop();
		}
	}
}
